package codename.carrot.bomberman.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import codename.carrot.bomberman.R;
import codename.carrot.bomberman.models.Player;

/**
 *  An adapter for displaying players (used in the game lobby):
 *  
 * @author Group A4
 *
 */
public class PlayerArrayAdapter extends ArrayAdapter<Player> {

	/**
	 * The layout inflator for inflating and updating the GUI.
	 */
	private LayoutInflater inflater;


	/**
	 * Constructor, constructing the PlayerArrayAdapter.
	 * @param context The calling context.
	 * @param resource The resource ID for a layout file containing a TextView to use when instantiating views.
	 * @param textViewResourceId The id of the TextView within the layout resource to be populated.
	 */
	public PlayerArrayAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * Setting the GUI-component to be displayed (view) and returning it.
	 * @see android.widget.ArrayAdapter#getView(int position, View convertView, ViewGroup parent)
	 * 
	 * @return The generated view.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {	    
	    View v;	    
	    if(convertView == null) {
	        v = inflater.inflate(R.layout.listview_entry, null);
	    }
	    else {
	        v = convertView;
	    }	    
		Player player = getItem(position);		
		TextView list = (TextView) v.findViewById(R.id.listview_entry_list);
		TextView listSub = (TextView) v.findViewById(R.id.listview_entry_sub);

		list.setText(player.getNickname());
		listSub.setText("PlayerID: " + player.getPlayerId());
		return v;
	}

}
